//
//  AddMaterialsViewController.m
//  SsehMaster
//
//  Created by 杨阳 on 2018/8/11.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "AddMaterialsViewController.h"
#import "MaterialViewController.h"
#import "SelectdMateralModel.h"
#import "SearchMaterialModel.h"
#import "OrderInforModel.h"
#import "MaterialChargeViewController.h"
#import "MaterialFreeViewController.h"
#import "ConfirmServiceViewController.h"

@interface AddMaterialsViewController ()<UIScrollViewDelegate,BackAddVc>

@property (nonatomic,strong) UIScrollView * scrollView;

@property (nonatomic,strong) UISegmentedControl * typeSgc;

@property (nonatomic,strong) MaterialChargeViewController * chargeVC;

@property (nonatomic,strong) MaterialFreeViewController * freeVC;


@end

@implementation AddMaterialsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self baseSetting];
    [self layoutUI];
}

-(void)baseSetting{
    self.navigationItem.title = @"确定服务";
    
}


-(void)layoutUI{
    
    __weak typeof (self)weakSelf = self;
    self.typeSgc = [[UISegmentedControl alloc] initWithItems:@[@"收费项", @"免费项"]];
    self.typeSgc.tintColor = [UIColor whiteColor];
    self.typeSgc.backgroundColor = [UIColor whiteColor];

    [self.typeSgc setSelectedSegmentIndex:0];
    [self.typeSgc setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:RGBColor(128, 128, 128)} forState:UIControlStateNormal];
    [self.typeSgc setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16], NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateSelected];
    [self.typeSgc addTarget:self action:@selector(typeSgcChangeValue:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.typeSgc];
    [self.typeSgc mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(50);
    }];
    UIView * lineView = [UIView new];
    lineView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.typeSgc.mas_top).offset(10);
        make.bottom.equalTo(weakSelf.typeSgc.mas_bottom).offset(-10);
        make.centerX.equalTo(weakSelf.typeSgc);
        make.width.mas_equalTo(1);
    }];
    
    
    
    UIButton * sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sureButton.titleLabel.font = [UIFont systemFontOfSize:16];
    sureButton.backgroundColor = [UIColor appColor];
    sureButton.layer.cornerRadius = 5;
    [sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(sureButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sureButton];
    [sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.equalTo(weakSelf.view).offset(-HOME_INDICATOR_HEIGHT-10);
        make.left.equalTo(weakSelf.view).offset(10);
        make.right.equalTo(weakSelf.view).offset(-10);
        make.height.mas_equalTo(50);
        
    }];
    
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,60, VIEW_WIDTH, VIEW_HEIGHT - 50 - NAVIGATION_BAR_HEIGHT - 80)];
    self.scrollView.backgroundColor = [UIColor backGroudColorForView];
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.contentSize = CGSizeMake(VIEW_WIDTH*2, VIEW_HEIGHT - 50 - NAVIGATION_BAR_HEIGHT - 80);
    self.scrollView.bounces = NO;
    [self.view addSubview:self.scrollView];
    
    self.chargeVC = [[MaterialChargeViewController alloc]init];
    if (!kArrayIsEmpty(self.oldChargDataSource)) {
        self.chargeVC.chargeDataSourcel = self.oldChargDataSource;
    }
    [self addChildViewController:self.chargeVC];
    
    [self.scrollView addSubview:self.chargeVC.view];
    
    self.freeVC = [[MaterialFreeViewController alloc]init];
    if (!kArrayIsEmpty(self.oldFreeDataSource)) {
        self.freeVC.freeDataSource = self.oldFreeDataSource;
    }
    [self addChildViewController:self.freeVC];
    self.freeVC.view.frame = CGRectMake(VIEW_WIDTH, 0, VIEW_WIDTH, self.scrollView.bounds.size.height);
    [self.scrollView addSubview:self.freeVC.view];
    
}

-(void)typeSgcChangeValue:(UISegmentedControl *)seg{
    
    switch (seg.selectedSegmentIndex) {
        case 0:
            [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
            break;
        case 1:
            [self.scrollView setContentOffset:CGPointMake(VIEW_WIDTH, 0) animated:YES];
            break;
            
        default:
            break;
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat x = self.scrollView.contentOffset.x;
    
    if (x == 0) {
        self.typeSgc.selectedSegmentIndex = 0;
    }else{
        self.typeSgc.selectedSegmentIndex = 1;
        
    }
}

-(void)sureButtonClick:(UIButton *)button{
    
    if (![self chargeStaty]) {
        if (self.chargeVC.selectMaterialModel == nil) {
            [self showTipWithTitle:@"请选择材料"];
            return;
        }
        
        if (kStringIsEmpty(self.chargeVC.numTf.text)) {
            [self showTipWithTitle:@"请输入材料数量"];
            return;
        }
        
        if ([self.chargeVC.numTf.text floatValue]<=0) {
            [self showTipWithTitle:@"请输入大于0的材料数量"];
            return;
        }
        
        if (kStringIsEmpty(self.chargeVC.priceTf.text)) {
            [self showTipWithTitle:@"请输入材料价格"];
            return;
        }
        
        SelectdMateralModel * model = [[SelectdMateralModel alloc]init];
        model.material_id = self.chargeVC.selectMaterialModel.material_id;
        model.order_material_id = self.chargeVC.selectMaterialModel.order_material_id;
        model.material_name = self.chargeVC.selectMaterialModel.material_name;
        model.material_count = self.chargeVC.numTf.text;
        model.material_price = self.chargeVC.priceTf.text;
        [self.chargeVC.chargeDataSourcel addObject:model];
        
        self.chargeVC.selectMaterialModel = nil;
        self.chargeVC.numTf.text = nil;
        self.chargeVC.priceTf.text = nil;
        self.chargeVC.materialsNameLabel.text = @"请选择材料";
        [self.chargeVC.ssehTableView reloadData];
        
        
    }
    
    if (![self freeStaty]) {
        if (self.freeVC.selectMaterialModel == nil) {
            [self showTipWithTitle:@"请选择材料"];
            return;
        }
        
        if (kStringIsEmpty(self.freeVC.numTf.text)) {
            [self showTipWithTitle:@"请输入材料数量"];
            return;
        }
        if ([self.freeVC.numTf.text integerValue]<=0) {
            [self showTipWithTitle:@"请输入大于0的整数的材料数量"];
            return;
        }
        SelectdMateralModel * model = [[SelectdMateralModel alloc]init];
        model.material_id = self.freeVC.selectMaterialModel.material_id;
        model.order_material_id = self.chargeVC.selectMaterialModel.order_material_id;
        model.material_name = self.freeVC.selectMaterialModel.material_name;
        model.material_count = self.freeVC.numTf.text;
        model.material_price = @"0";
        [self.freeVC.freeDataSource addObject:model];
        self.freeVC.selectMaterialModel = nil;
        self.freeVC.numTf.text = nil;
        self.freeVC.materialsNameLabel.text = @"请选择材料";
        [self.freeVC.ssehTableView reloadData];
    }
    
    if (kArrayIsEmpty(self.chargeVC.chargeDataSourcel) && kArrayIsEmpty(self.freeVC.freeDataSource)) {
        [self showTipWithTitle:@"未选择材料"];
    }
    
    
    NSMutableArray * meterialMArr = [[NSMutableArray alloc]initWithCapacity:0];
    if (!kArrayIsEmpty(self.chargeVC.chargeDataSourcel)) {
        
        [meterialMArr addObjectsFromArray:self.chargeVC.chargeDataSourcel];
        
    }
    if (!kArrayIsEmpty(self.freeVC.freeDataSource)) {
     
        [meterialMArr addObjectsFromArray:self.freeVC.freeDataSource];
    }
    
    for (NSInteger i = 0; i < meterialMArr.count; i++) {
        
        SelectdMateralModel * model = meterialMArr[i];
        
        if (kStringIsEmpty(model.material_name)) {
            [self showTipWithTitle:@"有材料信息不完整"];
            return;
        }
        
        if (kStringIsEmpty(model.material_count)) {
            [self showTipWithTitle:@"有已选材料的数量未填写"];
            return;
        }
        if (kStringIsEmpty(model.material_price)) {
            [self showTipWithTitle:@"有已选材料的价格未填写"];
            return;
        }
        
    }
    
    ConfirmServiceViewController * vc = [[ConfirmServiceViewController alloc]init];
    vc.delegate = self;
    vc.dataSource = meterialMArr;
    vc.chargeDataSource = self.chargeVC.chargeDataSourcel;
    vc.freeDataSource = self.freeVC.freeDataSource;
    vc.orderModel = self.orderModel;
    [self.navigationController pushViewController:vc animated:YES];
}



-(BOOL)chargeStaty{
    
    if (self.chargeVC.selectMaterialModel == nil && kStringIsEmpty(self.chargeVC.numTf.text) && kStringIsEmpty(self.chargeVC.priceTf.text)) {
        return YES;
    }
    return NO;
}

-(BOOL)freeStaty{
    
    if (self.freeVC.selectMaterialModel == nil && kStringIsEmpty(self.freeVC.numTf.text)) {
        return YES;
    }
    return NO;
    
}

-(void)addMaterials{
    
    self.chargeVC.ssehTableView.tableFooterView = self.chargeVC.footerView;
    self.freeVC.ssehTableView.tableFooterView = self.freeVC.footerView;
    
}

-(void)changeMaterials{
    self.chargeVC.ssehTableView.tableFooterView = nil;
    self.freeVC.ssehTableView.tableFooterView = nil;
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
